package com.atguigu.gmall.product.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/product/test2")
public class TestRedisController {
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/test")
    public void test(){
        HashOperations<String, String, Object> hashOps = redisTemplate.opsForHash();

        // 设置单个字段值
        hashOps.put("HASH_KEY", "field1", "value1");
        hashOps.put("HASH_KEY", "field2", "value2");

        // 获取指定字段的值
        Object fieldValue = hashOps.get("HASH_KEY", "field1");
        System.out.println("Field value for 'field1': " + fieldValue);

        // 删除指定字段
        hashOps.delete("HASH_KEY", "field2");

        // 判断Hash中是否包含指定字段
        boolean containsKey = hashOps.hasKey("HASH_KEY", "field3");
        System.out.println("Contains key 'field3'? " + containsKey);

        // 清空Hash
//        hashOps.clear();

    }
}
